/*
 *  $Id: Manager.cs 1199 2010-11-20 17:38:24Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;
using System.IO;

using AgateLib.DisplayLib;

namespace Ragima {
    /// <summary>
    /// Менеджер дополнительной информации об эффектах
    /// </summary>
    public static class AffectManager {
        private static Dictionary<Type, AffectInfo> _infos;

        static AffectManager() {
            _infos = new Dictionary<Type,AffectInfo>();
        }

        /// <summary>
        /// Получение информации об эффекте с автоматическим созданием при его отсутствии.
        /// </summary>
        public static AffectInfo GetInfo(Affect affect) {
            Type type = affect.GetType();
            AffectInfo info;
            if (!_infos.TryGetValue(type, out info)) {
                info = new AffectInfo(type, affect.Id);
                _infos.Add(type, info);
            }
            return info;
        }

        /// <summary>
        /// Получение идентификатора для эффекта, связанный с определенной картой.
        /// </summary>
        public static string GetCardId<T>(string suffix) where T: Card {
            Card card = CardManager.GetCard<T>();
            string id = card == null ? "/"+Affect.AffectPrefix+"unknown" : "/"+card.GetResourceId("affect");
            if (!string.IsNullOrEmpty(suffix)) id += "."+suffix;
            return id;
        }
    }

    /// <summary>
    /// Дополнительная информация об эффекте (идентификатор, название, описания, изображения)
    /// </summary>
    public class AffectInfo {
        public readonly Type AffectType;
        public readonly string Id;
        public readonly Languages.Phrase Name, Description, IconDescription;
        private bool _hasPic;
        private Surface _smallPic, _bigPic;

        public AffectInfo(Type affect, string id) {
            AffectType = affect;
            Id = id;
            Name = new Languages.Phrase(GetResourceId(string.Empty));
            Description = new Languages.Phrase(GetResourceId("desc"));
            IconDescription = new Languages.Phrase(GetResourceId("icondesc"));
            _hasPic = File.Exists(GetBigPictureName()) && File.Exists(GetSmallPictureName());
        }

        public string GetResourceId(string suffix) {
            string prefix = Id.StartsWith("/") ? Id.Substring(1) : Affect.AffectPrefix+Id;
            return string.IsNullOrEmpty(suffix) ? prefix : prefix+"."+suffix;
        }

        private FilePath GetBigPictureName() { return Paths.AffectImages[Id + ".jpg"]; }
        private FilePath GetSmallPictureName() { return Paths.AffectImages[Id + "_s.jpg"]; }

        public Surface BigPicture {
            get { return _hasPic ? (_bigPic ?? (_bigPic = new Surface(GetBigPictureName()))) : null; }
        }
        public Surface SmallPicture {
            get { return _hasPic ? (_smallPic ?? (_smallPic = new Surface(GetSmallPictureName()))) : null; }
        }
    }
}